package com.jian.bus.service.impl;

import com.jian.bus.pojo.Goods;
import com.jian.bus.pojo.Inport;
import com.jian.bus.mapper.InportMapper;
import com.jian.bus.service.GoodsService;
import com.jian.bus.service.InportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 剑
 * @since 2020-01-22
 */
@Service
@Transactional
public class InportServiceImpl extends ServiceImpl<InportMapper, Inport> implements InportService {
    @Autowired
    private GoodsService goodsService;
    /**
     * 进货保存时商品数量增加 需要总数量=商品数量+进货数量
     * @param entity
     * @return
     */
    @Override
    public boolean save(Inport entity) {
        //更新商品数量
        Goods goods = goodsService.getById(entity.getGoodsid());
        goods.setNumber(goods.getNumber()+entity.getNumber());
        goodsService.updateById(goods);
        return super.save(entity);
    }

    /**
     * 当修改进货表 需要改变商品数量
     * 原有 1000 添加500 修改为400
     * 现在总数量=1000-500+400
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(Inport entity) {
        //查询原来的进货数量
        Inport oldInport = this.baseMapper.selectById(entity.getGoodsid());
        //查询商品数量
        Goods goods = goodsService.getById(entity.getGoodsid());
        //设置商品数量----->商品数量-原来添加的数量+现在修改的数量
        goods.setNumber(goods.getNumber()-oldInport.getNumber()+entity.getNumber());
        goodsService.updateById(goods);
        return super.updateById(entity);
    }
    /**
     * 删除进货信息需要在商品中减掉
     */
    @Override
    public boolean removeById(Serializable id) {
        //查询原来的进货数量
        Inport oldInport = this.baseMapper.selectById(id);
        //查询商品数量
        Goods goods = goodsService.getById(oldInport.getGoodsid());
        //设置商品数量----->商品数量-进货单的数量
        goods.setNumber(goods.getNumber()-oldInport.getNumber());
        goodsService.updateById(goods);
        return super.removeById(id);
    }
}
